Inserting First
Inserting to the front is much less costly because you're pretty much just as creating a new pointer to the head of the list and returning the new head.
node *add(node *list, int num) {
node *new_node = malloc(sizeof(node)); // create a new node using the memory allocation function
// check if the malloc failed, for instance, the system has ran out of memory to allocate
if (new_node == NULL) {
printf("malloc failed");
return list;
}
new_node->value = num; // set the value of the node
new_node->next = list; // assign the next pointer to be a pointer to the previous head
return new_node;
}
Inserting Last
You have to traverse the entire linked list to add to the end which can be costly if the length of the list is very large.